﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Engine
{
    /// <summary>
    /// Updates the Keyboard once per Update().
    /// </summary>
    public sealed class KeyboardComponent
    {
        KeyboardState state, prevState;

        #region Constructor

        public KeyboardComponent()
            : base()
        {

        }

        #endregion

        /// <summary>
        /// True if the key is pressed.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool IsKeyDown(Keys key)
        {
            return state.IsKeyDown(key);
        }

        /// <summary>
        /// True if the key is not pressed.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool IsKeyUp(Keys key)
        {
            return state.IsKeyUp(key);
        }

        /// <summary>
        /// True is key is pressed but wasn't in the previous Update.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool IsKeyJustDown(Keys key)
        {
            return state.IsKeyDown(key) && prevState.IsKeyUp(key);
        }

        /// <summary>
        /// True is key is pressed but wasn't in the previous Update.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool IsKeyJustUp(Keys key)
        {
            return state.IsKeyUp(key) && prevState.IsKeyDown(key);
        }

        public void Update(GameTime gameTime)
        {
#if !XBOX
            //if (GameBT.console.InputEnabled) //FIX ME!.
            //{
            //    prevState = state = new KeyboardState();
            //}
            //else
            {
                prevState = state;
                state = Keyboard.GetState();
            }
#endif
        }
    }
}
